package com.qding.park.manager.app.queue.handle;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.qding.park.common.core.constant.MongodbConstant;
import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.core.redis.SerializeUtils;
import com.qding.park.manager.pojo.TbParkspace;
import com.qding.park.manager.pojo.ap.ApDayReportInfo;
import com.qding.park.manager.pojo.noncore.NcPassDayReport;
import com.qding.park.manager.pojo.queue.QueueMsg;
import com.qding.park.manager.service.queue.mapper.TbParkspaceMapper;

/**
 * @ClassName: PassDayReportHandle
 * @Description:通行日报 记录上报外理
 * @author Comsys-Administrator
 * @date 2016年10月20日 上午10:10:31
 *
 */
@Component("passDayReportHandle")
public class PassDayReportHandle extends AbstractReportHandle {

	@Autowired
	private MongoTemplate mongoTemplate;
	
	@Autowired
	private TbParkspaceMapper tbParkspaceMapper;

	@Override
	void actualHandle(QueueMsg msg) throws BaseException {
		JSONObject data = (JSONObject) msg.getData();
		ApDayReportInfo apDayReportInfo = SerializeUtils.unSerialize(data.toJSONString(), ApDayReportInfo.class);
		
		String passDayReportTabelName = MongodbConstant.TABLENAME_PASS_DAY_REPORT;

		NcPassDayReport ncPassDayReport = new NcPassDayReport();
		ncPassDayReport.setCalcMoney(StringUtils.isBlank(apDayReportInfo.getCalcMoney()) ? null
				: Integer.valueOf(apDayReportInfo.getCalcMoney()));
		ncPassDayReport.setCalcMonthPay(StringUtils.isBlank(apDayReportInfo.getCalcMonthPay()) ? null
				: Integer.valueOf(apDayReportInfo.getCalcMonthPay()));
		ncPassDayReport.setCalcSaveCardPay(StringUtils.isBlank(apDayReportInfo.getCalcSaveCardPay()) ? null
				: Integer.valueOf(apDayReportInfo.getCalcSaveCardPay()));
		ncPassDayReport.setDuring(StringUtils.isBlank(apDayReportInfo.getDuring()) ? null
				: Integer.valueOf(apDayReportInfo.getDuring()));
		ncPassDayReport.setEnManual(StringUtils.isBlank(apDayReportInfo.getEnManual()) ? null
				: Integer.valueOf(apDayReportInfo.getEnManual()));
		ncPassDayReport.setEnterTimes(StringUtils.isBlank(apDayReportInfo.getEnterTimes()) ? null
				: Integer.valueOf(apDayReportInfo.getEnterTimes()));
		ncPassDayReport.setFeeAheadMoney(StringUtils.isBlank(apDayReportInfo.getFeeAheadMoney()) ? null
				: Integer.valueOf(apDayReportInfo.getFeeAheadMoney()));
		ncPassDayReport.setFreeTimes(StringUtils.isBlank(apDayReportInfo.getFreeTimes()) ? null
				: Integer.valueOf(apDayReportInfo.getFreeTimes()));
		ncPassDayReport.setLostCard(StringUtils.isBlank(apDayReportInfo.getLostCard()) ? null
				: Integer.valueOf(apDayReportInfo.getLostCard()));
		ncPassDayReport.setMoney(StringUtils.isBlank(apDayReportInfo.getMoney()) ? null
				: Integer.valueOf(apDayReportInfo.getMoney()));
		ncPassDayReport.setMonthPay(StringUtils.isBlank(apDayReportInfo.getMonthPay()) ? null
				: Integer.valueOf(apDayReportInfo.getMonthPay()));
		ncPassDayReport.setNoCardTimes(StringUtils.isBlank(apDayReportInfo.getNoCardTimes()) ? null
				: Integer.valueOf(apDayReportInfo.getNoCardTimes()));
		ncPassDayReport.setOffPay(StringUtils.isBlank(apDayReportInfo.getOffPay()) ? null
				: Integer.valueOf(apDayReportInfo.getOffPay()));
		ncPassDayReport.setoPCode(apDayReportInfo.getoPCode());
		ncPassDayReport.setoPName(apDayReportInfo.getoPName());
		ncPassDayReport.setoPTime(apDayReportInfo.getoPTime());
		ncPassDayReport.setOutManual(StringUtils.isBlank(apDayReportInfo.getOutManual()) ? null
				: Integer.valueOf(apDayReportInfo.getOutManual()));
		ncPassDayReport.setOutTimes(StringUtils.isBlank(apDayReportInfo.getOutTimes()) ? null
				: Integer.valueOf(apDayReportInfo.getOutTimes()));
		ncPassDayReport.setPayCard(StringUtils.isBlank(apDayReportInfo.getPayCard()) ? null
				: Integer.valueOf(apDayReportInfo.getPayCard()));
		ncPassDayReport.setSaveCardPay(StringUtils.isBlank(apDayReportInfo.getSaveCardPay()) ? null
				: Integer.valueOf(apDayReportInfo.getSaveCardPay()));
		ncPassDayReport.setSupplyID(apDayReportInfo.getSupplyID());
		ncPassDayReport.setParkCode(apDayReportInfo.getClientID());
		TbParkspace tbParkspace = tbParkspaceMapper.queryParkInfoByCodeSupplier(apDayReportInfo.getClientID(), Integer.valueOf(apDayReportInfo.getSupplyID()));
		ncPassDayReport.setParkId(tbParkspace.getId());
		ncPassDayReport.setParkName(tbParkspace.getName());
		ncPassDayReport.setCreate_time(new Date());
//		mongoTemplate.save(ncPassDayReport, passDayReportTabelName);
		
		
		Criteria matchCrieria = new Criteria();
		matchCrieria.and("parkId").is(ncPassDayReport.getParkId());
		matchCrieria.and("oPTime").is(ncPassDayReport.getoPTime());
		matchCrieria.and("oPCode").is(ncPassDayReport.getoPCode());
		matchCrieria.and("oPName").is(ncPassDayReport.getoPName());
		Query query = Query.query(matchCrieria);
		
		Update  update=new Update();
		update.set("supplyID", ncPassDayReport.getSupplyID());
		update.set("parkCode", ncPassDayReport.getParkCode());
		update.set("parkId", ncPassDayReport.getParkId());
		update.set("oPTime", ncPassDayReport.getoPTime());
		update.set("oPCode", ncPassDayReport.getoPCode());
		update.set("oPName", ncPassDayReport.getoPName());
		update.set("during", ncPassDayReport.getDuring());
		update.set("outTimes", ncPassDayReport.getOutTimes());
		update.set("lostCard", ncPassDayReport.getLostCard());
		update.set("enterTimes", ncPassDayReport.getEnterTimes());
		update.set("payCard", ncPassDayReport.getPayCard());
		update.set("money", ncPassDayReport.getMoney());
		update.set("offPay", ncPassDayReport.getOffPay());
		update.set("monthPay", ncPassDayReport.getMonthPay());
		update.set("saveCardPay", ncPassDayReport.getSaveCardPay());
		update.set("enManual", ncPassDayReport.getEnManual());
		update.set("outManual", ncPassDayReport.getOutManual());
		update.set("freeTimes", ncPassDayReport.getFreeTimes());
		update.set("noCardTimes", ncPassDayReport.getNoCardTimes());
		update.set("feeAheadMoney", ncPassDayReport.getFeeAheadMoney());
		update.set("calcMonthPay", ncPassDayReport.getCalcMonthPay());
		update.set("calcSaveCardPay", ncPassDayReport.getCalcSaveCardPay());
		update.set("calcMoney", ncPassDayReport.getCalcMoney());
		update.set("parkName", ncPassDayReport.getParkName());
		update.set("create_time", new Date());
		mongoTemplate.upsert(query, update, passDayReportTabelName);
	}

	@Override
	boolean failDiscardRollBacK(QueueMsg msg) {
		_log.error("车场进场消息回滚:" + msg);
		return true;
	}

}
